import AntModal from "@/components/pt/dialog/AntModal";
import {
  getBizCustomerFilter,
  addBizCustomerFilter,
  updateBizCustomerFilter,
  findMaxSort,
} from "@/api/biz/bizCustomerFilter";

export default {
  name: "CreateForm",
  props: {
    carStatusOptions: {
      type: Array,
      required: true,
    },

    levelOptions: {
      type: Array,
      required: true,
    },

    progressOptions: {
      type: Array,
      required: true,
    },
    cityOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加筛选客户条件",
      // 表单参数
      form: {},
      rules: {},
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        cityName: undefined,

        carStatus: undefined,

        level: undefined,

        progress: undefined,

        pgPrice: undefined,

        maxCount: undefined,

        shareCount: undefined,

        filterType: 0,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加筛选客户条件";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizCustomerFilterId = row.id;
      getBizCustomerFilter(bizCustomerFilterId).then((response) => {
        if (response.data.carStatus !== null) {
          response.data.carStatus = JSON.parse(response.data.carStatus);
        } else {
          response.data.carStatus = [];
        }

        if (response.data.progress !== null) {
          response.data.progress = JSON.parse(response.data.progress);
        } else {
          response.data.progress = [];
        }
        this.form = response.data;
        this.formTitle = "修改筛选客户条件";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (saveForm.carStatus) {
            saveForm.carStatus = JSON.stringify(saveForm.carStatus);
          } else {
            saveForm.carStatus = null;
          }

          if (saveForm.progress) {
            saveForm.progress = JSON.stringify(saveForm.progress);
          } else {
            saveForm.progress = null;
          }
          if (this.form.id !== undefined) {
            updateBizCustomerFilter(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizCustomerFilter(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/customerfilter/index";
      this.$router.push(index);
    },
  },
};
